package swd.basic.conf_prg_button;

import java.util.*;

import javax.servlet.http.*;

import com.pub.database.*;

public class Business {
    QueryBean gqbnDAO = null;
    public Business(QueryBean pqbnDAO) {
        gqbnDAO = pqbnDAO;
    }

    /**
     * getData
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    String getData(View view, String pstrUserNO) throws Exception {
        if (view.getData_kind().equals("getDataViewHtml")) {
            return getDataViewHtml(view.getDataView(), view.getDataViewForm(),
                                   view);
        }

        return "";
    }

    String getDataViewHtml(DataView dataView, DataViewForm DataViewForm,
                           View view) throws Exception {
        //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
        DataViewForm.setPageActionURI(getDataViewPageAction(view));

        //設定要query的table list(以逗號隔開)
        DataViewForm.setTablesList(" wkf_prg_button ");

        //依欄位查詢值組合sql where
        DataViewForm.setSQLCondition(getDataViewWhere(view));

        //設定排序的欄位
        DataViewForm.setOrderByFields("special_name_e");

        //排序的方法(正序或倒序)
        DataViewForm.setOrderBySeq("asc");

        //group by 的欄位(以逗號隔開)
        DataViewForm.setGroupBy(null);

        //Having 的欄位(有設定group by時此欄位才有作用)
        DataViewForm.setHaving(null);

        //將欄位相關設定值設入DataViewForm
        DataViewForm.setColRelationUseArrayList(getDataViewColRelation(dataView,
                view));

        //設定DataView Header的<tr> html屬性
        DataViewForm.setTableHeaderHtml(
                "class='td_head_grid' align='center'  width='500px'");

        //DataViewForm.

        DataViewForm.setJumpPageButtonShow(false); //是否要秀出上下頁按鈕
        DataViewForm.setPageBtnOnTop(false); //將button and rows info 秀在頂端
        DataViewForm.setPageBtnOnButtom(true); //將button and rows info 秀在底端
        DataViewForm.setFilterShow(false); //是否要秀出過濾選擇欄位
        DataViewForm.setJumpPageShow(false); //是否要秀出跳頁下拉式選單
        DataViewForm.setRowsInfoShow(false); //是否要秀出筆數資訊
        DataViewForm.setRowPageShow(false); //是否要秀出每頁筆數控制欄
        DataViewForm.setExcelShow(false); //是否要秀出轉Excel button
        DataViewForm.setOrderEnable(false); //是否要提供排序點選功能
        DataViewForm.setShowHeaderWhenNoData(true); //當ResultSet無資料是否要秀Header
        DataViewForm.setAddNewShow(true); //是否秀出新增鈕
        DataViewForm.setBatchUpdateButtonShow(true); //是否秀出批次更新鈕(需在jsp自行增加evt_dataview_btnBatchUpdate_onclick() js function)

        if(DataViewForm.getAddNewShow()){
            view.setDataViewAppendRowScript(dataView.getAppendRowHtml(DataViewForm,DataViewForm.getColRelationUseArrayList()));
        }

        return dataView.getOutputDataView(DataViewForm);
    }

    /**
     * getDataViewColRelation
     *
     * @param dataView DataView
     * @param view View
     * @return ArrayList
     */
    private ArrayList<Hashtable<String,Object>> getDataViewColRelation(DataView dataView, View view) {
        ArrayList<Hashtable<String,Object>> laltColRelation = new ArrayList<Hashtable<String,Object>>();
        Hashtable<String,Object> lhtColRelationDetail = new Hashtable<String,Object>();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "'Del' as del");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'Del'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "width='3%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                                 "<input type='checkbox' name='willDel' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "'%sn%'");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='checkbox' name='willDel' value='' onclick='if(this.checked){this.value=(DataViewTableBody.rows.length/3)-1}'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "SPECIAL_NAME_E");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'按鈕名稱'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='special_name_e' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "SPECIAL_NAME_E");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='special_name_e' value=''/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "SPECIAL_NAME");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'按鈕顯示文字'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='special_name' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "SPECIAL_NAME");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='special_name' value=''/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "ACTION_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'顯示條件'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "TEXTAREA");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<TEXTAREA cols='60' rows='5' name='action_cndt'>");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "ACTION_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<TEXTAREA cols='60' rows='5' name='action_cndt'></textarea>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "CLICK_ACTION");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'JAVASCRIPT'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "TEXTAREA");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<TEXTAREA cols='60' rows='5' name='click_action'>");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "CLICK_ACTION");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<TEXTAREA cols='60' rows='5' name='click_action'></textarea>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "CLICK_ACTION_POST");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'JAVA'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "TEXTAREA");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<TEXTAREA cols='60' rows='5' name='click_action_post'>");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "CLICK_ACTION_POST");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<TEXTAREA cols='60' rows='5' name='click_action_post'></textarea>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        return laltColRelation;
    }

    /**
     * getDataViewPageAction
     *
     * @param view View
     * @return String
     */
    private String getDataViewPageAction(View view) {
        StringBuffer stbPageActionURI = new StringBuffer(view.getRequest().getRequestURI() + "?prg_no=");

        stbPageActionURI.append(view.getPrg_no() == null ? "" :
                                view.getPrg_no());

        return stbPageActionURI.toString();
    }

    /**
     * getDataViewWhere
     *
     * @param view View
     * @return Object
     */
    private String getDataViewWhere(View view) throws Exception {
        StringBuffer stbCondition = new StringBuffer();

        stbCondition.append(" prg_no='");
        stbCondition.append(view.getPrg_no());
        stbCondition.append("'");

        return stbCondition.toString();
    }

    /**
     * batchUpdateAction
     *
     * @param view View
     * @param pstrUserNO String
     */
    String batchUpdateAction(View view, String pstrUserNO,
                             HttpServletRequest request) throws Exception {
        //String[] lstrRowSn = view.getChangeRowNum().split(",");
        StringBuffer lstbResult = new StringBuffer();

        try {


            //lstbResult.append("儲存成功!!");
            return "";

        } catch (Exception e) {
            lstbResult.append(" is duplicate");
            return lstbResult.toString();
        } finally {
            if (!lstbResult.toString().trim().equals("")) {
                view.setOnloadScript("alert('" + lstbResult.toString() +
                                     "');");
            } else {
                endAction(view);
            }
        }
    }

    String exeDelSQL(View view, int i, String pstrUserNO) throws Exception {
        String lstrSQL = new String();
        Vector<Object> lvtrParam=new Vector<Object>();
        try {
            lvtrParam.add(view.getPrg_no());

            lstrSQL = "delete wkf_prg_button where prg_no=? and special_name_e";
            if (!gqbnDAO.executeSQL(lstrSQL, lvtrParam)) {
                return "ams_trans_pcc";
            }
            lvtrParam.clear();

            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        } finally {
            lvtrParam.clear();
        }
    }



    String exeUpdateSQL(View view, int i, String pstrUserNO) throws Exception {
        return "";
    }

    /**
     * initAction
     *
     * @param view View
     * @param pstrUserNO String
     */
    void endAction(View view) throws Exception {

    }

    /**
     * initAction
     *
     * @param view View
     */
    void initAction(View view) throws Exception{

    }
}
